﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ABMath.ModelFramework.Data;
using GisEntity;

namespace GisWCFService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Service1”。
    public class GISService : IGISService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="timeseries"></param>
        /// <returns></returns>
        private MyTimeSeries GetMyTSFromTS(TimeSeries timeseries)
        {
            MyTimeSeries myTimeseries = new MyTimeSeries();
            myTimeseries.Titel = timeseries.Title;
            for (int i = 0; i < timeseries.Count; i++)
            {
                myTimeseries.Times.Add(timeseries.TimeStamp(i));
                myTimeseries.Values.Add(Convert.ToDecimal(timeseries.ValueAtTime(timeseries.TimeStamp(i))));
            }
            return myTimeseries;
        }
        /// <summary>
        /// 获得所有地市的GDP数据
        /// </summary>
        /// <returns></returns>
        public List<MyTimeSeries> GetTimeSeriesList()
        {
            // throw new NotImplementedException();
            List<TimeSeries> TSList = new List<TimeSeries>();
            using (HnMapEntities databes = new HnMapEntities())
            {
                var queryCityResult = from cities in databes.市界_REGION
                                      select cities.NAME;
                List<string> CityList = new List<string>();
                CityList = queryCityResult.ToList();
                foreach (var city in CityList)
                {
                    List<DateTime> dt = new List<DateTime>();
                    List<Decimal> values = new List<Decimal>();
                    var queryGDPResult = from gdp in databes.GDPTEST
                                         where gdp.CITYNAME.Equals(city)
                                         select gdp;
                    // Console.WriteLine(queryGDPResult.Count());
                    if (queryGDPResult.Count() > 0)
                    {

                        foreach (var gdpitem in queryGDPResult.ToList())
                        {
                            //Console.WriteLine("{0}'GDP in {1} is : {2}", city, gdpitem.TIME, gdpitem.GDPVALUE);
                            dt.Add(gdpitem.TIME);
                            values.Add(gdpitem.GDPVALUE);
                        }
                        TSList.Add(TimeSeries.GetTSFromList(city, dt, values, false));
                    }

                }
                List<MyTimeSeries> MyTSList = new List<MyTimeSeries>();
                foreach (var item in TSList)
                {
                    //Console.WriteLine(item.Title);
                    MyTimeSeries tempMyTS = new MyTimeSeries();
                    tempMyTS.Titel = item.Title;
                    for (int i = 0; i < item.Count; i++)
                    {
                        // Console.WriteLine("{0} in {1} is: {2}", item.Title, item.TimeStamp(i), item.ValueAtTime(item.TimeStamp(i)));
                        tempMyTS.Times.Add(item.TimeStamp(i));
                        tempMyTS.Values.Add(Convert.ToDecimal(item.ValueAtTime(item.TimeStamp(i))));
                    }
                    MyTSList.Add(tempMyTS);

                }
                return MyTSList;
            }
        }


        public MyTimeSeries GetTimeSericesByName(string name)
        {
            throw new NotImplementedException();
             
        }

        public List<MyTimeSeries> GetTimeSericesByTime(DateTime time)
        {
            throw new NotImplementedException();
        }
    }
}
